package com.irdstudio.efp.nls.service.impl.queue;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.nls.service.dao.NlsProcessRuleDao;
import com.irdstudio.efp.nls.service.domain.queue.NlsProcessRule;
import com.irdstudio.efp.nls.service.facade.queue.NlsProcessRuleService;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessRuleVO;

/**
 * ServiceImpl: 网贷规则过程
 * 
 * @author huangwb
 * @since 2018年12月27日 上午8:33:47
 * @version 1.0
 */
@Service("nlsProcessRuleService")
public class NlsProcessRuleServiceImpl implements NlsProcessRuleService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(NlsProcessRuleServiceImpl.class);

	@Autowired
	private NlsProcessRuleDao nlsProcessRuleDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertNlsProcessRule(NlsProcessRuleVO inNlsProcessRuleVo) {
		logger.debug("当前新增数据为:" + inNlsProcessRuleVo.toString());
		int num = 0;
		try {
			NlsProcessRule nlsProcessRule = new NlsProcessRule();
			beanCopy(inNlsProcessRuleVo, nlsProcessRule);
			num = nlsProcessRuleDao.insertNlsProcessRule(nlsProcessRule);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}
	
	/**
	 * @description:批量插入
	 * @param nlsProcessRules
	 * @return
	 * @date:2019年1月7日 上午9:54:56
	 * @author:hengyh
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertByList(List<NlsProcessRuleVO> nlsProcessRules) {
		logger.debug("当前尝试新增数据条数为:" + nlsProcessRules.size());
		int num = 0;
		try {
			List<NlsProcessRule> nlsProcessRuleList = 
					(List<NlsProcessRule>) beansCopy(nlsProcessRules, NlsProcessRule.class);
			num = nlsProcessRuleDao.insertByList(nlsProcessRuleList);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(NlsProcessRuleVO inNlsProcessRuleVo) {
		logger.debug("当前删除数据条件为:" + inNlsProcessRuleVo);
		int num = 0;
		try {
			NlsProcessRule nlsProcessRule = new NlsProcessRule();
			beanCopy(inNlsProcessRuleVo, nlsProcessRule);
			num = nlsProcessRuleDao.deleteByPk(nlsProcessRule);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsProcessRuleVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(NlsProcessRuleVO inNlsProcessRuleVo) {
		logger.debug("当前修改数据为:" + inNlsProcessRuleVo.toString());
		int num = 0;
		try {
			NlsProcessRule nlsProcessRule = new NlsProcessRule();
			beanCopy(inNlsProcessRuleVo, nlsProcessRule);
			num = nlsProcessRuleDao.updateByPk(nlsProcessRule);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsProcessRuleVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public NlsProcessRuleVO queryByPk(NlsProcessRuleVO inNlsProcessRuleVo) {

		logger.debug("当前查询参数信息为:" + inNlsProcessRuleVo);
		try {
			NlsProcessRule queryNlsProcessRule = new NlsProcessRule();
			beanCopy(inNlsProcessRuleVo, queryNlsProcessRule);
			NlsProcessRule queryRslNlsProcessRule = nlsProcessRuleDao.queryByPk(queryNlsProcessRule);
			if (Objects.nonNull(queryRslNlsProcessRule)) {
				NlsProcessRuleVO outNlsProcessRuleVo = beanCopy(queryRslNlsProcessRule, new NlsProcessRuleVO());
				logger.debug("当前查询结果为:" + outNlsProcessRuleVo.toString());
				return outNlsProcessRuleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsProcessRuleVO> queryAllOwner(NlsProcessRuleVO nlsProcessRuleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<NlsProcessRuleVO> list = null;
		try {
			List<NlsProcessRule> nlsProcessRules = nlsProcessRuleDao.queryAllOwnerByPage(nlsProcessRuleVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + nlsProcessRules.size());
			pageSet(nlsProcessRules, nlsProcessRuleVo);
			list = (List<NlsProcessRuleVO>) beansCopy(nlsProcessRules, NlsProcessRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsProcessRuleVO> queryAllCurrOrg(NlsProcessRuleVO nlsProcessRuleVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<NlsProcessRule> nlsProcessRules = nlsProcessRuleDao.queryAllCurrOrgByPage(nlsProcessRuleVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + nlsProcessRules.size());
		List<NlsProcessRuleVO> list = null;
		try {
			pageSet(nlsProcessRules, nlsProcessRuleVo);
			list = (List<NlsProcessRuleVO>) beansCopy(nlsProcessRules, NlsProcessRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsProcessRuleVO> queryAllCurrDownOrg(NlsProcessRuleVO nlsProcessRuleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<NlsProcessRule> nlsProcessRules = nlsProcessRuleDao.queryAllCurrDownOrgByPage(nlsProcessRuleVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + nlsProcessRules.size());
		List<NlsProcessRuleVO> list = null;
		try {
			pageSet(nlsProcessRules, nlsProcessRuleVo);
			list = (List<NlsProcessRuleVO>) beansCopy(nlsProcessRules, NlsProcessRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 将T-1的数据插入到历史表中
	 * 
	 * @param currAppDate
	 * @return
	 * @author huangwb
	 * @since 2019年1月3日 上午10:40:45
	 * @version 1.0
	 */
	@Override
	public int deleteTMinus1DData(String currAppDate) {
		logger.debug("当前删除数据条件为:" + currAppDate);
		int num = 0;
		try {
			num = nlsProcessRuleDao.deleteTMinus1DData(currAppDate);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + currAppDate + "删除的数据条数为" + num);
		return num;
	}


}
